package cn.itcast.service.impl;

import cn.itcast.dao.CountyDao;
import cn.itcast.domain.County;
import cn.itcast.service.CountyService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.util.List;

public class CountyServiceImpl implements CountyService {

    @Override
    public PageInfo<County> queryPage(Integer pageNum, Integer pageSize) throws Exception {
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml")).openSession(true);
        CountyDao countyDao = sqlSession.getMapper(CountyDao.class);

        //1.设置分页
        PageHelper.startPage(pageNum, pageSize);
        //2.查询数据
        List<County> list = countyDao.findAll();
        //3.封装pageInfo
        PageInfo<County> pageInfo = new PageInfo<>(list);

        sqlSession.close();
        return pageInfo;
    }
}
